﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlTypes;

namespace DataAccess.Operate
{
    public class DA_Project_SalseAgency_Inherit:SqlInteractionBase
    {
        public object SelectOneByMainID(string mainID)
        {
            string sSql = "select * from t_Project_SalseAgency where Project_SalseAgency_MainID='" + mainID + "'";
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = sSql;
            DataTable toReturn = new DataTable("t_Project_Lease");
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            DataEntity.T_Project_SalseAgency _objMessage = new DataEntity.T_Project_SalseAgency();
            try
            {

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                adapter.Fill(toReturn);
                if (toReturn.Rows.Count > 0)
                {
                    _objMessage.Project_SalseAgency_ID = (Guid)toReturn.Rows[0]["Project_SalseAgency_ID"];
                    _objMessage.Project_SalseAgency_MainID = (Guid)toReturn.Rows[0]["Project_SalseAgency_MainID"];
                    _objMessage.Project_SalseAgency_TypeID = (int)toReturn.Rows[0]["Project_SalseAgency_TypeID"];
                    _objMessage.Project_SalseAgency_FirstA1 = toReturn.Rows[0]["Project_SalseAgency_FirstA1"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_SalseAgency_FirstA1"];
                    _objMessage.Project_SalseAgency_FirstA2 = toReturn.Rows[0]["Project_SalseAgency_FirstA2"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_SalseAgency_FirstA2"];
                    _objMessage.Project_SalseAgency_FirstA3 = toReturn.Rows[0]["Project_SalseAgency_FirstA3"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_SalseAgency_FirstA3"];
                    _objMessage.Project_SalseAgency_FirstB1 = toReturn.Rows[0]["Project_SalseAgency_FirstB1"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_SalseAgency_FirstB1"];
                    _objMessage.Project_SalseAgency_FirstB2 = toReturn.Rows[0]["Project_SalseAgency_FirstB2"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_SalseAgency_FirstB2"];
                }
                return _objMessage;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Project_Lease::SelectOne::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
                adapter.Dispose();
            }
        }

        public bool DeleteByMainID(string id)
        {
            string sSql = "delete from t_Project_SalseAgency where  Project_SalseAgency_MainID='" + id + "'";
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = sSql;
            cmdToExecute.Connection = _mainConnection;
            try
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("", ex);
            }
            finally
            {

                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }
    }
}
